package cn.koala.key.stock.service.impl;

import cn.koala.key.api.KeyManagementApi;
import cn.koala.key.common.excel.FileUtil;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.stock.domain.dto.CarVinDTO;
import cn.koala.key.stock.domain.dto.NewCarDTO;
import cn.koala.key.stock.domain.vo.KeyInfoVO;
import cn.koala.key.stock.service.KeyInfoService;
import cn.seed.common.core.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 钥匙业务
 * @description
 * @author shuai.xu
 * @time 2019/5/5 9:34
*/
@Service
public class KeyInfoServiceImpl implements KeyInfoService {


    @Override
    public Result<List<KeyInfoVO>> scanCode(List<String> keyBarCode,Integer status) {
        Result<List<KeyInfoVO>> keyInfoResult = FeignUtils.getHttpApi(KeyManagementApi.class).scanCode(keyBarCode,status);
        if (ResultCode.SUCCESS.getCode() != keyInfoResult.getErrorCode() || null == keyInfoResult.getResult()){
            throw new ServiceException(ResultCode.FAIL,keyInfoResult.getMessage());
        }
        return keyInfoResult;
    }

    @Override
    public Result<List<KeyInfoVO>> scrapedScanCode(List<String> keyBarCode) {
        Result<List<KeyInfoVO>> keyInfoResult = FeignUtils.getHttpApi(KeyManagementApi.class).scrapedScanCode(keyBarCode);
        if (ResultCode.SUCCESS.getCode() != keyInfoResult.getErrorCode() || null == keyInfoResult.getResult()){
            throw new ServiceException(ResultCode.FAIL,keyInfoResult.getMessage());
        }
        return keyInfoResult;
    }

    @Override
    public Result reportLoss(String keyBarCode, String dropReason) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).reportLoss(keyBarCode,dropReason);
    }

    @Override
    public Result inStock(String keyBarCode) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).inStock(keyBarCode);
    }

    @Override
    public Result<List<KeyInfoVO>> getByCarNoAndCarVin(String carVin, String carNo, int num) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).getByCarNoAndCarVin(carVin, carNo, num);
    }

    @Override
    public Result<List<KeyInfoVO>> getByCarNoAndCarVin(String carVin, String carNo) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).getByCarNoAndCarVin(carVin, carNo);
    }

    @Override
    public Result<List<KeyInfoVO>> batchSelectKeyByCarVinList(MultipartFile file) {
        List<CarVinDTO> carVinList = null;
        try {
            carVinList = FileUtil.importExcel(file, 0, 1, CarVinDTO.class);
        } catch (Exception e) {
            return ResultGenerator.genFailResult(ResultCode.FAIL, "导入excel异常！");
        }
        if(null==carVinList||carVinList.isEmpty()){
            throw new ServiceException(ResultCode.BAD_REQUEST,  "excel数据异常请检查！！！");
        }
        return FeignUtils.getHttpApi(KeyManagementApi.class).batchSelectKeyByCarVinList(carVinList);
    }
}
